from util.Service import *
# 获取所有的日志信息(分页 所有信息)
def getLogAll(page:Page = Page(),hType:str= '',cdn:dict=dict()):
    if(hType == ''):
        return Log.getAllHandleLog(page,cdn)
    h = Menu.getHandleNameByType(hType)
    hids = [item.get('hId') for item in h]
    return Log.getAllHandleLog(page,cdn,hids)

# 日志导出
def exportLog(hType:str= '',cdn:dict=dict()):
    h = Menu.getHandleNameByType(hType)
    hids = [item.get('hId') for item in h]
    return Log.getAllHandleLog(None,cdn,hids)

# 修改日志信息（除了lid）
def updateLog(id:int,k):
    return Log.updateHandleLog(id,k)
    
# 删除日志信息
def delLog(lids:list[int]):
    return Log.clearLog(lids)

# 清空日志
def clearLog(hType:str,cdn:dict):
    h = Menu.getHandleNameByType(hType)
    hids = [item.get('hId') for item in h]
    Log.clearLog(hids,cdn)
    return True

# 增加日志信息
def addLog(username,token,resp,status, ip,path,req,method):
    handle:dict = Menu.getHandleByPathAndMethod(path,method)
    if(handle==None):
        return
    if(handle.get("status") == "0"):
        return
    if(handle.get("log") == "0"):
        return
    log = {
        "handleName": handle.get("name"),
        "time": datetime.utcnow(),
        "key":handle.get("key"),
        "resp": resp,
        "username":username,
        "status" : status,
        "ip" : ip,
        "token":  token,
        "path": path,
        "req":req,
        "hid":handle.get('hId')
    }
    return Log.addHandleLog(log)
